define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/index' + location.search,
                    add_url: 'course/add',
                    edit_url: 'course/edit',
                    del_url: 'course/del',
                    packages_url: 'course/packages',
                    multi_url: 'course/multi',
                    import_url: 'course/import',
                    table: 'course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'people',
                            title: __('People'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'show_switch',
                            title: __('Show_switch'),
                            table: table,
                            formatter: Table.api.formatter.toggle
                        },
                        // {
                        //     field: 'st_switch',
                        //     title: __('St_switch'),
                        //     table: table,
                        //     formatter: Table.api.formatter.toggle
                        // },
                        {field: 'st_id', title: __('St_id')},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {
                            field: 'flag',
                            title: __('Flag'),
                            searchList: {
                                "normal": __('Flag normal'),
                                "hot": __('Flag hot'),
                                "recommend": __('Flag recommend')
                            },
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'keyword',
                            title: __('Keyword'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'hours',
                            title: __('Hours'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {field: 'video.title', title: __('Video.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'video.url', title: __('Video.url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: '查看课包',
                                    text: __('查看课包'),
                                    title: __('查看课包'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'course/packages',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                // {
                                //     name: 'ajax',
                                //     text: __('发送Ajax'),
                                //     title: __('发送Ajax'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     icon: 'fa fa-magic',
                                //     url: 'example/bootstraptable/detail',
                                //     confirm: '确认发送',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                //         //如果需要阻止成功提示，则必须使用return false;
                                //         //return false;
                                //     },
                                //     error: function (data, ret) {
                                //         console.log(data, ret);
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     }
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('新选项卡中打开'),
                                //     title: __('新选项卡中打开'),
                                //     classname: 'btn btn-xs btn-warning btn-addtabs',
                                //     icon: 'fa fa-folder-o',
                                //     url: 'example/bootstraptable/detail'
                                // }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        packages: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/packages?cid='+Config.cids,
                    table: 'package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder:"desc",
                showToggle: false,showColumns: false,showExport: false,commonSearch: false,search:false,
                pagination:false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'nums',
                            title: __('视频数'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: '选择课包',
                                    text: __('选择课包'),
                                    title: __('选择课包'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-list',
                                    confirm:"是否选择课包",
                                    url:"course/chackPackage/ids/{id}/cid/"+Config.cids,
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.is_check==1) return false;
                                        return true;
                                    },
                                    success:function (res){
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: '取消选择',
                                    text: __('取消选择'),
                                    title: __('取消选择'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: 'fa fa-list',
                                    confirm:"是否取消选择",
                                    url:"course/chackPackage/ids/{id}/cid/"+Config.cids,
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.is_check==1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        // packages: function () {
        //     Controller.api.bindevent();
        // },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
